package com.qf.service.impl;

import com.qf.mapper.*;
import com.qf.model.*;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.service.ProducterTaskService;
import com.qf.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ProducterTaskServiceImpl implements ProducterTaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private FarmMapper farmMapper;
    @Autowired
    private ProductBatchesMapper productBatchesMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ProducterMapper producterMapper;


    @Override
    @Transactional
    public int add(TTask task) {
        task.setCreateDate(new Date());
        return taskMapper.add(task);



    }

    @Override
    public TTask findById(int id) {
        return taskMapper.findById(id);
    }


    @Override
    @Transactional
    public int edit(TTask task) {

        int i = taskMapper.edit(task);
        return i;
    }


    @Override
    @Transactional
    public int delete(int id) {
        return taskMapper.delete(id);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {


        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = null;
        if (principal instanceof UserDetails) {
            username = ((UserDetails)principal).getUsername();
        } else {
            username = principal.toString();
        }
        System.out.println("username---------->"+username);

        Integer producterId = producterMapper.findByName(username).getId();


        Integer CurrentPage = queryPageBean.getCurrentPage();
        Integer PageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult pageResult = new PageResult();
        List<TTask> rows = taskMapper.findPage2((CurrentPage - 1) * PageSize, PageSize, queryString,producterId);
        System.out.println("rows"+rows);
        for (TTask row : rows) {
            System.out.println("farmid"+row.getFarmId());
            Farm farm = farmMapper.findById(row.getFarmId());
            row.setFarm(farm);
            TProducter producter = producterMapper.findById(row.getProducterId());
            row.setProducter(producter);
            SysUser sysUser = sysUserMapper.findById(row.getSysUserId());
            row.setSysUser(sysUser);
            ProductBatches productBatches = productBatchesMapper.findById(row.getProductBatchesId());
            row.setProductBatches(productBatches);

        }
        Long total = taskMapper.count2(queryString,producterId);
        pageResult.setRows(rows);
        pageResult.setTotal(total);
        return pageResult;
    }
}
